package com.im.partchat.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.im.partchat.R;
import com.im.partchat.bean.User;
import com.im.partchat.ui.fragment.contact.ContactFragment;
import com.im.partchat.widget.GlideCircleTransform;

import java.util.List;

public class ContactAdapter extends BaseAdapter{

	private List<User> memsList = null;
	private Context context = null;
	private boolean showWord = true;
    private boolean showCheckBox = false;
	private boolean checkBoxEnable = true;

	public ContactAdapter(List<User> memsList, Context context) {
		this.memsList = memsList;
		this.context = context;
	}

    public boolean isShowCheckBox() {
        return showCheckBox;
    }

    public void setShowCheckBox(boolean showCheckBox) {
        this.showCheckBox = showCheckBox;
    }

    @Override
	public int getCount() {
		if (this.memsList == null) {
			return 0;
		}
		return this.memsList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.memsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.contact_list_item, null);
			holder.wordLayout = (LinearLayout)convertView.findViewById(R.id.txl_word_layout);
			holder.word = (TextView)convertView.findViewById(R.id.txl_word);
			holder.name = (TextView)convertView.findViewById(R.id.cylxr_txl_list_item_name);
			holder.mobile = (TextView)convertView.findViewById(R.id.cylxr_txl_list_item_mobile);
			holder.dept = (TextView)convertView.findViewById(R.id.cylxr_txl_list_item_dept);
			holder.img = (ImageView)convertView.findViewById(R.id.offline_txl_list_item_icon);
			holder.img_mark = (ImageView) convertView.findViewById(R.id.avator_mark);
            holder.checkBox = (CheckBox)convertView.findViewById(R.id.cylxr_checkbox);
			holder.bottomLine = convertView.findViewById(R.id.bottom_line);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final User address = this.memsList.get(position);
		if (address != null) {
			holder.name.setText(address.getName());
			if (showWord) {
				String currentStr = ContactFragment.getAlpha(address.getJp()); // 姓名首字母
				String previewStr = (position - 1) >= 0 ? ContactFragment.getAlpha(this.memsList.get(
						position - 1).getJp()) : " ";
				if (!previewStr.equals(currentStr)) {
					holder.wordLayout.setVisibility(View.VISIBLE);
					holder.word.setVisibility(View.VISIBLE);
					holder.word.setText(currentStr);
				} else {
					holder.wordLayout.setVisibility(View.GONE);
					holder.word.setVisibility(View.GONE);
				}
			} else {
				holder.word.setVisibility(View.GONE);
				holder.wordLayout.setVisibility(View.GONE);
			}
            if (isShowCheckBox()) {
                holder.checkBox.setVisibility(View.VISIBLE);
            } else {
                holder.checkBox.setVisibility(View.GONE);
            }
            holder.checkBox.setChecked(address.isChoosed());
			holder.checkBox.setEnabled(address.isEnable());
			holder.mobile.setText(address.getPhone());
			if (!TextUtils.isEmpty(address.getName())) {
				holder.name.setText(address.getName());
			}else {
				holder.name.setText("");
			}
			if (!TextUtils.isEmpty(address.getPosition())) {
				holder.dept.setText(address.getPosition());
			}else {
				holder.dept.setText("");
			}
			if (!TextUtils.isEmpty(address.getHeadUrl())) {
				Glide.with(context)
						.load(address.getHeadUrl())
						.transform(new GlideCircleTransform(context))
						.placeholder(R.drawable.rc_ic_def_msg_portrait)
						.error(R.drawable.rc_ic_def_msg_portrait)
						.diskCacheStrategy(DiskCacheStrategy.ALL)
						.into(holder.img);
			} else {
				holder.img.setImageResource(R.drawable.rc_ic_def_msg_portrait);
			}
			if (isShowCheckBox()) {
				holder.checkBox.setVisibility(View.VISIBLE);
			}else{
				holder.checkBox.setVisibility(View.GONE);
			}

			if (position == memsList.size()-1){
				holder.bottomLine.setVisibility(View.VISIBLE);
			} else {
				holder.bottomLine.setVisibility(View.GONE);
			}
		}
		return convertView;
	}
	
	public void setWordVisible(boolean show){
		showWord = show;
	}
	class ViewHolder {
		LinearLayout wordLayout;
		TextView word;
		TextView name;
		TextView mobile;
        TextView dept;
        ImageView img, img_mark;
        CheckBox checkBox;
		View bottomLine;
	}

}
